package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Rectangle;
import java.text.DecimalFormat;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

public class GraphPanel extends JPanel {

	private static final long serialVersionUID = 1L;
	private JPanel WattPanel = null;
	private JPanel TimePanel = null;
	private JPanel Time1 = null;
	private JPanel Time2 = null;
	private JPanel Time3 = null;
	private JPanel Time4 = null;
	private JPanel Time5 = null;
	private JPanel Time6 = null;
	private JPanel Time7 = null;
	private JPanel Time8 = null;
	private JPanel Time9 = null;
	private JPanel Time10 = null;
	private JPanel Time11 = null;
	private JPanel Time12 = null;
	private JPanel Watt1 = null;
	private JPanel Watt2 = null;
	private JPanel Watt3 = null;
	private JPanel Watt4 = null;
	private JPanel Watt5 = null;
	private JPanel Watt6 = null;
	private JLabel tl1 = null;
	private JLabel tl2 = null;
	private JLabel tl3 = null;
	private JLabel tl4 = null;
	private JLabel tl5 = null;
	private JLabel tl6 = null;
	private JLabel tl7 = null;
	private JLabel tl8 = null;
	private JLabel tl9 = null;
	private JLabel tl10 = null;
	private JLabel tl11 = null;
	private JLabel tl12 = null;
	private JLabel wl1 = null;
	private JLabel wl2 = null;
	private JLabel wl3 = null;
	private JLabel wl4 = null;
	private JLabel wl5 = null;
	private JLabel wl6 = null;
	private JLabel TitleLabel = null;
	private JLabel Peak = null;
	private JLabel Average = null;
	private JLabel Total = null;
	private JFrame fr;

	/**
	 * This is the default constructor
	 */
	public GraphPanel(String table, JFrame f) {
		super();
		fr = f;
		initialize(table);
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */

	public void setTitleText(String s) {
		TitleLabel.setText("Consumption Graph for " + s);
	}

	public void setData(String peak, String average, String total) {
		Peak.setText("Peak Consumption: " + peak + " kWh");
		Average.setText("Average Consumption: " + average + " kWh");
		Total.setText("Total Consumption: " + total + " kWh ");
	}

	private void initialize(String table) {
		String values[] = table.split("_");
		System.out.println(values.length);
		String data = new String();
		data = values[3] + "-" + values[4] + "-" + values[5];
		DecimalFormat df = new DecimalFormat("#0.000");
		Total = new JLabel();
		Total.setBounds(new Rectangle(480, 530, 220, 25));
		Average = new JLabel();
		Average.setBounds(new Rectangle(260, 530, 220, 25));
		Peak = new JLabel();
		Peak.setBounds(new Rectangle(40, 530, 220, 25));
		Total.setForeground(new Color(0, 0, 102));
		Average.setForeground(new Color(0, 0, 102));
		Peak.setForeground(new Color(0, 0, 102));
		TitleLabel = new JLabel();
		TitleLabel.setBounds(new Rectangle(100, 10, 520, 25));
		TitleLabel.setFont(new Font("SansSerif", Font.BOLD | Font.ITALIC, 14));
		setTitleText("");
		TitleLabel.setHorizontalAlignment(SwingConstants.CENTER);
		TitleLabel.setForeground(new Color(0, 0, 102));
		this.setLayout(null);
		this.setBounds(new Rectangle(0, 0, 710, 575));
		this.setBackground(new Color(250, 250, 250));
		this.add(getTimePanel(), null);
		this.add(getWattPanel(), null);
		Graph g = new Graph(table);
		this.add(TitleLabel, null);
		JPanel p = g.getPanel();
		ScreenCapture s = new ScreenCapture(fr);
		MousePopupListener PopupListener = new MousePopupListener();
		PopupListener.setParent(this);
		PopupListener.setPopup(s.getRoomPopup());
		this.addMouseListener(PopupListener);
		this.add(p);
		setData(df.format(g.getMax()), df.format(g.getAvg()), df.format(g
				.getTotal()));
		this.add(Peak, null);
		this.add(Average, null);
		this.add(Total, null);
		setTitleText(data);

	}

	/**
	 * This method initializes GraphPanel
	 * 
	 * @return javax.swing.JPanel
	 */

	/**
	 * This method initializes WattPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getWattPanel() {
		if (WattPanel == null) {
			WattPanel = new JPanel();
			WattPanel.setLayout(null);
			WattPanel.setBackground(Color.white);
			WattPanel.setBounds(new Rectangle(30, 40, 50, 450));
			WattPanel.add(getWatt1(), null);
			WattPanel.add(getWatt2(), null);
			WattPanel.add(getWatt3(), null);
			WattPanel.add(getWatt4(), null);
			WattPanel.add(getWatt5(), null);
			WattPanel.add(getWatt6(), null);
		}
		return WattPanel;
	}

	/**
	 * This method initializes TimePanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getTimePanel() {
		if (TimePanel == null) {
			TimePanel = new JPanel();
			TimePanel.setLayout(null);
			TimePanel.setBounds(new Rectangle(80, 490, 600, 20));
			TimePanel.setBackground(Color.white);
			TimePanel.add(getTime1(), null);
			TimePanel.add(getTime2(), null);
			TimePanel.add(getTime3(), null);
			TimePanel.add(getTime4(), null);
			TimePanel.add(getTime5(), null);
			TimePanel.add(getTime6(), null);
			TimePanel.add(getTime7(), null);
			TimePanel.add(getTime8(), null);
			TimePanel.add(getTime9(), null);
			TimePanel.add(getTime10(), null);
			TimePanel.add(getTime11(), null);
			TimePanel.add(getTime12(), null);
		}
		return TimePanel;
	}

	/**
	 * This method initializes Time1
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getTime1() {
		if (Time1 == null) {
			tl1 = new JLabel();
			tl1.setText(" 00:00");
			Time1 = new JPanel();
			Time1.setLayout(new BorderLayout());
			Time1.setBounds(new Rectangle(0, 0, 50, 20));
			Time1.setBackground(Color.white);
			Time1
					.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY,
							1));
			Time1.add(tl1, BorderLayout.CENTER);
		}
		return Time1;
	}

	/**
	 * This method initializes Time2
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getTime2() {
		if (Time2 == null) {
			tl2 = new JLabel();
			tl2.setText(" 02:00");
			Time2 = new JPanel();
			Time2.setLayout(new BorderLayout());
			Time2.setBounds(new Rectangle(50, 0, 50, 20));
			Time2.setBackground(Color.white);
			Time2
					.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY,
							1));
			Time2.add(tl2, BorderLayout.CENTER);
		}

		return Time2;
	}

	/**
	 * This method initializes Time3
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getTime3() {
		if (Time3 == null) {
			tl3 = new JLabel();
			tl3.setText(" 04:00");
			Time3 = new JPanel();
			Time3.setLayout(new BorderLayout());
			Time3.setBounds(new Rectangle(100, 0, 50, 20));
			Time3.setBackground(Color.white);
			Time3
					.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY,
							1));
			Time3.add(tl3, BorderLayout.CENTER);
		}
		return Time3;
	}

	/**
	 * This method initializes Time4
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getTime4() {
		if (Time4 == null) {
			tl4 = new JLabel();
			tl4.setText(" 06:00");
			Time4 = new JPanel();
			Time4.setLayout(new BorderLayout());
			Time4.setBounds(new Rectangle(150, 0, 50, 20));
			Time4.setBackground(Color.white);
			Time4
					.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY,
							1));
			Time4.add(tl4, BorderLayout.CENTER);
		}
		return Time4;
	}

	/**
	 * This method initializes Time5
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getTime5() {
		if (Time5 == null) {
			tl5 = new JLabel();
			tl5.setText(" 08:00");
			Time5 = new JPanel();
			Time5.setLayout(new BorderLayout());
			Time5.setBounds(new Rectangle(200, 0, 50, 20));
			Time5.setBackground(Color.white);
			Time5
					.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY,
							1));
			Time5.add(tl5, BorderLayout.CENTER);
		}
		return Time5;
	}

	/**
	 * This method initializes Time6
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getTime6() {
		if (Time6 == null) {
			tl6 = new JLabel();
			tl6.setText(" 10:00");
			Time6 = new JPanel();
			Time6.setLayout(new BorderLayout());
			Time6.setBounds(new Rectangle(250, 0, 50, 20));
			Time6.setBackground(Color.white);
			Time6
					.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY,
							1));
			Time6.add(tl6, BorderLayout.CENTER);
		}
		return Time6;
	}

	/**
	 * This method initializes Time7
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getTime7() {
		if (Time7 == null) {
			tl7 = new JLabel();
			tl7.setText(" 12:00");
			Time7 = new JPanel();
			Time7.setLayout(new BorderLayout());
			Time7.setBounds(new Rectangle(300, 0, 50, 20));
			Time7.setBackground(Color.white);
			Time7
					.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY,
							1));
			Time7.add(tl7, BorderLayout.CENTER);
		}
		return Time7;
	}

	/**
	 * This method initializes Time8
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getTime8() {
		if (Time8 == null) {
			tl8 = new JLabel();
			tl8.setText(" 14:00");
			Time8 = new JPanel();
			Time8.setLayout(new BorderLayout());
			Time8.setBounds(new Rectangle(350, 0, 50, 20));
			Time8.setBackground(Color.white);
			Time8
					.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY,
							1));
			Time8.add(tl8, BorderLayout.CENTER);
		}
		return Time8;
	}

	/**
	 * This method initializes Time9
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getTime9() {
		if (Time9 == null) {
			tl9 = new JLabel();
			tl9.setText(" 16:00");
			Time9 = new JPanel();
			Time9.setLayout(new BorderLayout());
			Time9.setBounds(new Rectangle(400, 0, 50, 20));
			Time9.setBackground(Color.white);
			Time9
					.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY,
							1));
			Time9.add(tl9, BorderLayout.CENTER);
		}
		return Time9;
	}

	/**
	 * This method initializes Time10
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getTime10() {
		if (Time10 == null) {
			tl10 = new JLabel();
			tl10.setText(" 18:00");
			Time10 = new JPanel();
			Time10.setLayout(new BorderLayout());
			Time10.setBounds(new Rectangle(450, 0, 50, 20));
			Time10.setBackground(Color.white);
			Time10.setBorder(BorderFactory
					.createLineBorder(Color.LIGHT_GRAY, 1));
			Time10.add(tl10, BorderLayout.CENTER);
		}
		return Time10;
	}

	/**
	 * This method initializes Time11
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getTime11() {
		if (Time11 == null) {
			tl11 = new JLabel();
			tl11.setText(" 20:00");
			Time11 = new JPanel();
			Time11.setLayout(new BorderLayout());
			Time11.setBounds(new Rectangle(500, 0, 50, 20));
			Time11.setBackground(Color.white);
			Time11.setBorder(BorderFactory
					.createLineBorder(Color.LIGHT_GRAY, 1));
			Time11.add(tl11, BorderLayout.CENTER);
		}
		return Time11;
	}

	/**
	 * This method initializes Time12
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getTime12() {
		if (Time12 == null) {
			tl12 = new JLabel();
			tl12.setText(" 22:00");
			Time12 = new JPanel();
			Time12.setLayout(new BorderLayout());
			Time12.setBounds(new Rectangle(550, 0, 50, 20));
			Time12.setBackground(Color.white);
			Time12.setBorder(BorderFactory
					.createLineBorder(Color.LIGHT_GRAY, 1));
			Time12.add(tl12, BorderLayout.CENTER);
		}
		return Time12;
	}

	/**
	 * This method initializes Watt1
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getWatt1() {
		if (Watt1 == null) {
			wl1 = new JLabel();
			wl1.setText(" 4500 W");
			wl1.setVerticalAlignment(SwingConstants.TOP);
			Watt1 = new JPanel();
			Watt1.setLayout(new BorderLayout());
			Watt1.setBounds(new Rectangle(0, 0, 50, 75));
			Watt1.setBackground(Color.white);
			Watt1
					.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY,
							1));
			Watt1.add(wl1, BorderLayout.CENTER);
		}
		return Watt1;
	}

	/**
	 * This method initializes Watt2
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getWatt2() {
		if (Watt2 == null) {
			wl2 = new JLabel();
			wl2.setText(" 3750 W");
			wl2.setVerticalAlignment(SwingConstants.TOP);
			Watt2 = new JPanel();
			Watt2.setLayout(new BorderLayout());
			Watt2.setBounds(new Rectangle(0, 75, 50, 75));
			Watt2.setBackground(Color.white);
			Watt2
					.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY,
							1));
			Watt2.add(wl2, BorderLayout.CENTER);
		}
		return Watt2;
	}

	/**
	 * This method initializes Watt3
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getWatt3() {
		if (Watt3 == null) {
			wl3 = new JLabel();
			wl3.setText(" 3000 W");
			wl3.setVerticalAlignment(SwingConstants.TOP);
			Watt3 = new JPanel();
			Watt3.setLayout(new BorderLayout());
			Watt3.setBounds(new Rectangle(0, 150, 50, 75));
			Watt3.setBackground(Color.white);
			Watt3
					.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY,
							1));
			Watt3.add(wl3, BorderLayout.CENTER);
		}
		return Watt3;
	}

	/**
	 * This method initializes Watt4
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getWatt4() {
		if (Watt4 == null) {
			wl4 = new JLabel();
			wl4.setText(" 2250 W");
			wl4.setVerticalAlignment(SwingConstants.TOP);
			Watt4 = new JPanel();
			Watt4.setLayout(new BorderLayout());
			Watt4.setBounds(new Rectangle(0, 225, 50, 75));
			Watt4.setBackground(Color.white);
			Watt4
					.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY,
							1));
			Watt4.add(wl4, BorderLayout.CENTER);
		}
		return Watt4;
	}

	/**
	 * This method initializes Watt5
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getWatt5() {
		if (Watt5 == null) {
			wl5 = new JLabel();
			wl5.setText(" 1500 W");
			wl5.setVerticalAlignment(SwingConstants.TOP);
			Watt5 = new JPanel();
			Watt5.setLayout(new BorderLayout());
			Watt5.setBounds(new Rectangle(0, 300, 50, 75));
			Watt5.setBackground(Color.white);
			Watt5
					.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY,
							1));
			Watt5.add(wl5, BorderLayout.CENTER);
		}
		return Watt5;
	}

	/**
	 * This method initializes Watt6
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getWatt6() {
		if (Watt6 == null) {
			wl6 = new JLabel();
			wl6.setText("  750 W");
			wl6.setVerticalAlignment(SwingConstants.TOP);
			Watt6 = new JPanel();
			Watt6.setLayout(new BorderLayout());
			Watt6.setBounds(new Rectangle(0, 375, 50, 75));
			Watt6.setBackground(Color.white);
			Watt6
					.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY,
							1));
			Watt6.add(wl6, BorderLayout.CENTER);
		}
		return Watt6;
	}

} // @jve:decl-index=0:visual-constraint="10,10"
